package com.bdmc.web.controller.report;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.report.domain.bo.ReportCarStopBo;
import com.bdmc.report.domain.vo.ReportCarOilCostVo;
import com.bdmc.report.domain.vo.ReportCarStopVo;
import com.bdmc.report.service.IReportCarStopService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 怠速时长以及油耗控制器
 *
 * @author bdmc
 * @date 2022-08-09
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/report/carOilCost")
public class ReportCarOilCostController extends BaseController {

    private final IReportCarStopService iReportCarStopService;

    /**
     * 查询怠速时长以及油耗列表
     */
    @SaCheckPermission("report:carOil:list")
    @GetMapping("/list")
    public TableDataInfo<ReportCarStopVo> list(@Validated(QueryGroup.class) ReportCarStopBo bo, PageQuery pageQuery) {
        return iReportCarStopService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出怠速时长以及油耗列表
     */
    @SaCheckPermission("report:carOil:export")
    @Log(title = "怠速时长以及油耗", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ReportCarStopBo bo, HttpServletResponse response) {
        List<ReportCarOilCostVo> list = iReportCarStopService.queryOilList(bo);
        ExcelUtil.exportExcel(list, "车辆怠速油耗统计", ReportCarOilCostVo.class, response);
    }

    /**
     * 获取怠速时长以及油耗详细信息
     */
    @SaCheckPermission("report:carStop:query")
    @GetMapping("/{stopId}")
    public AjaxResult<ReportCarStopVo> getInfo(@PathVariable("stopId") Long stopId) {
        return AjaxResult.success(iReportCarStopService.queryById(stopId));
    }

    /**
     * 新增怠速时长以及油耗
     */
    @SaCheckPermission("report:carStop:add")
    @Log(title = "怠速时长以及油耗", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ReportCarStopBo bo) {
        return toAjax(iReportCarStopService.insertByBo(bo));
    }

    /**
     * 修改怠速时长以及油耗
     */
    @SaCheckPermission("report:carStop:edit")
    @Log(title = "怠速时长以及油耗", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ReportCarStopBo bo) {
        return toAjax(iReportCarStopService.updateByBo(bo));
    }

    /**
     * 删除怠速时长以及油耗
     */
    @SaCheckPermission("report:carStop:remove")
    @Log(title = "怠速时长以及油耗" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{stopIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] stopIds) {
        return toAjax(iReportCarStopService.deleteWithValidByIds(Arrays.asList(stopIds), true));
    }
}
